package com.zut.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.zut.dao.PermissionDao;
import com.zut.dao.RoleDao;
import com.zut.dao.UserDao;
import com.zut.pojo.Permission;
import com.zut.pojo.Role;
import com.zut.pojo.User;
import com.zut.service.OrderService;
import com.zut.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;

/**
 * 用户服务
 * @author sq
 * @create 2021-07-17-11:20
 */
//添加事务注解之后，必须明确当前这个服务实现的是哪一个服务接口
@Service(interfaceClass = UserService.class)
//事务注解
@Transactional
//实现health_interface中的接口
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private PermissionDao permissionDao;

    //根据用户名查询数据库获取用户信息和关联的角色信息，同时需要查询角色关联的权限信息
    public User findByUsername(String username) {
        User user = userDao.findByUsername(username);
        if(user == null){
            return null;
        }
        Integer userId = user.getId();
        //根据用户ID查询对应的角色
        Set<Role> roles =  roleDao.findByUserId(userId);
        for (Role role : roles) {
            Integer roleId = role.getId();
            //根据角色ID查询对应的权限
            Set<Permission> permissions =  permissionDao.findByRoleId(roleId);
            //让角色关联权限
            role.setPermissions(permissions);
        }
        //让用户关联角色
        user.setRoles(roles);
        return user;
    }
}
